package com.csi.controller;

import com.csi.domain.User;
import com.csi.service.UserService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("token")
public class TokenController {

    @Autowired
    private UserService userService;

    @RequestMapping("checkToken")
    public Object checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null || token.isEmpty()) {
            return new JsonResult(ReturnResults.ACCESS_FAILED, "未提供token！");
        }
        long userId = JwtUtil.getTokenBody(token).get("userId", Long.class);
        //根据userid查询用户信息返回
        User user = userService.getUser(userId);
        if (userId > 0  ) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS,user,"登录！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"未登录！");
    }
}
